package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.PageResponse;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @author Tangyin
 * @description查看商品详情
 * @since 2022/05/19 15:58
 */
@RestController
public class ShoppingProductController {
    @Autowired
    IProductService iProductService;

    @GetMapping("/shopping/product/{productId}")
    public ResponseData getProductDetail(@PathVariable("productId") Long productId){
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(productId);

       ProductDetailResponse productDetail =iProductService.getProductDetail(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(productDetail.getCode())){
            ProductDetailDto result =productDetail.getProductDetailDto();
            return new ResponseUtil().setData(result);
        }
        return new ResponseUtil().setErrorMsg(productDetail.getMsg());
    }


    @GetMapping("/shopping/recommend")
    public ResponseData recommend() {
        RecommendResponse recommendResponse=iProductService.getRecommendGoods();
        if (ShoppingRetCode.SUCCESS.getCode().equals(recommendResponse.getCode())) {
            Set<PanelDto> panelContentItemDtos = recommendResponse.getPanelContentItemDtos();
            return new ResponseUtil().setData(panelContentItemDtos);
        }
        return new ResponseUtil().setErrorMsg(recommendResponse.getMsg());
    }

    @GetMapping("/shopping/goods")
    public ResponseData goods(AllProductRequest request) {
        AllProductResponse allProductResponse = iProductService.getAllProduct(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(allProductResponse.getCode())) {
            List<ProductDto> productDtoList = allProductResponse.getProductDtoList();
            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(productDtoList);
            pageResponse.setTotal(allProductResponse.getTotal());
            return new ResponseUtil().setData(pageResponse);
        }
        return new ResponseUtil().setErrorMsg(allProductResponse.getMsg());
    }

}
